// PGLTextPave.cpp: implementation of the CPGLTextPave class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <pgl/core/PGLTextPave.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


PGL_IMPLEMENT_DYNCREATE(CPGLTextPave, CPGLPave);

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//! Save settings
void CPGLTextPave::SerializeXML(CPGLArchive& ar)
{
	static const TCHAR szTag[] = _T("CPGLTextPave");
	CString str;

	markup::CMarkupArchive& xml = *((markup::CMarkupArchive*)ar.GetMarkup());
	if(ar.IsStoring())
	{
		xml.AddChildElem(szTag);
		xml.AddChildAttrib(_T("Name"),GetName());
		xml.IntoElem();

			/////////////////////////////////////////////////////////
			// Adding base class
			CPGLPave::SerializeXML(ar);

			xml.AddChildElemEx(_T("Text"));
			xml.IntoElem();
				m_text.SerializeXML(ar);
			xml.OutOfElem();

		xml.OutOfElem();
	}
	else
	{
		if  (!xml.FindChildElem(szTag))
				return;

		xml.IntoElem();
			/////////////////////////////////////////////////////////
			// Adding base class
			CPGLPave::SerializeXML(ar);

			if (xml.FindChildElem(_T("Text")))
			{
				xml.IntoElem();
					m_text.SerializeXML(ar);
				xml.OutOfElem();
			}

		xml.OutOfElem();
	}
}


#ifdef _DEBUG
void CPGLTextPave::Dump( CDumpContext& dc ) const
{
    // call base class function first
    CPGLPave::Dump( dc );

}
void CPGLTextPave::AssertValid() const
{
    // call inherited AssertValid first
    CPGLPave::AssertValid();

	m_text.AssertValid();
} 
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPGLTextPave::CPGLTextPave(LPCTSTR szString, const CPGLColor& textColor, const CPGLColor& paveColor, const CPGLColor& fillColor)
: CPGLPave(paveColor, fillColor,CPGLPave::BorderLowerRightShadow), m_text(szString)
{
}

CPGLTextPave::~CPGLTextPave()
{

}

CPGLTextPave& CPGLTextPave::operator=(const CPGLTextPave& t)
{
	if (&t!=this)
	{
		this->CPGLPave::operator =(t);

		m_text=t.m_text;
	}
	return *this;
}

void CPGLTextPave::PlotGfx(gfxinterface::CGfxInterface& gfx)
{
	// update size...
	FitPaveToText(gfx);

	CPGLPave::PlotGfx(gfx);

	m_text.PlotGfx(gfx);
}

void CPGLTextPave::FitPaveToText(gfxinterface::CGfxInterface& gfx)
{
	using namespace gfxinterface;

	// first measure the text...
	m_text.UpdateExtent(gfx);

	// update pave dimensions...
	GrowInsideBBAbsToOutsideNormBBox(gfx, m_text.GetPixelWidth(), m_text.GetPixelHeight());

	// getting area position
	CPGLRectD rect=GetArea().GetWorldBBox(gfx);

	// moving text
	m_text.GetPosition().SetPosition( rect.left, rect.bottom, CPGLCoord2D::Coord2DWorld);
	m_text.GetPosition().ClearTrans();

	CPGLRectD rectBorder=GetTotalBorderSize();
	m_text.GetPosition().SetBBTrans( SPointD(rectBorder.left, rectBorder.bottom) );
}

void CPGLTextPave::GetUnionExtent(gfxinterface::CGfxInterface& gfx, CPGLRectD& rExtent)
{
	rExtent.xValid=false;
	rExtent.yValid=false;
}
